package com.yanqu.road.dao.impl.activity.tongbing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingCampData;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingCommanderData;
import com.yanqu.road.entity.activity.tongbing.data.CrossTongBingRedPacket;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTongBingCampDataDaoImpl extends TempDataObjectDao<CrossTongBingCampData> {

    public static String getCrossTongBingCampDataSql = "select * from `t_c_tongbing_camp_data` where `activityId`=? and groupId=? ";

    public Map<Integer, CrossTongBingCampData> getCrossTongBingCampDataMap(int activityId, int groupId) {
        Map<Integer, CrossTongBingCampData> campDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossTongBingCampDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossTongBingCampData campData = parse(rs);
                campDataMap.put(campData.getCampId(), campData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return campDataMap;
    }

    public static String updateCrossTongBingCampDataSql = "update `t_c_tongbing_camp_data` set " +
            "collectionData=?,redPacketData=?,redPacketValue=?,commanderData=? where activityId=? AND groupId=? AND campId=?";

    @Override
    protected boolean updateData(CrossTongBingCampData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCollectionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRedPacketMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRedPacketValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCommanderData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        if (getDb().executeNoneQuery(updateCrossTongBingCampDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossTongBingCampDataSql = "insert into `t_c_tongbing_camp_data`" +
            "(activityId,groupId,campId,collectionData,redPacketData,redPacketValue,commanderData) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossTongBingCampData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCollectionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRedPacketMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRedPacketValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getCommanderData())));
        if (getDb().executeNoneQuery(insertCrossTongBingCampDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossTongBingCampData parseData(ResultSet rs) throws SQLException {
        CrossTongBingCampData data = new CrossTongBingCampData();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setCampId(rs.getInt("campId"));
        data.setRedPacketValue(rs.getLong("redPacketValue"));
        data.setCollectionMap(JSON.parseObject(rs.getString("collectionData"), new TypeReference<ConcurrentHashMap<Long, ConcurrentHashMap<Integer, Long>>>() {
        }));
        data.setRedPacketMap(JSON.parseObject(rs.getString("redPacketData"), new TypeReference<ConcurrentHashMap<Long, CrossTongBingRedPacket>>() {
        }));
        data.setCommanderData(JSON.parseObject(rs.getString("commanderData"), new TypeReference<CrossTongBingCommanderData>() {
        }));
        return data;
    }

}
